﻿/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 13-8-29
 * Time: 下午3:34
 * To change this template use File | Settings | File Templates.
 */

$(document).ready(function () {
    var Book = Backbone.Model.extend({
        defaults: {
            title: 'default'
        },

        initialize: function () {
            $.log('Hey,you create me!');
        }
    });

    var BookShelf = Backbone.Collection.extend({
        model: Book
    });

    var book1 = new Book({title: "book1"});
    var book2 = new Book({title: "book2"});
    var book3 = new Book({title: "book3"});

    var bookShelf = new BookShelf();
    bookShelf.add(book1);
    bookShelf.add(book2);
    bookShelf.add(book3);
    bookShelf.remove(book3);

    /*
     for(var i=0; i<bookShelf.models.length; i++) {
     alert(bookShelf.models[i].get('title'));
     }
     */

    $("#testVal").click(function () {
        var showAllBooks=function(){
        bookShelf.each(function(book){
            $.log("--"+book.get("title")+"--");
          });
        };
        bookShelf.bind('reset',showAllBooks);
        bookShelf.fetch({
            url:"save.php",
            success:function(collection,response){
                collection.each(function(book){
                    alert(book.get('title'));
                });
            },
            error:function(){
                alert("error");
            }
        });

    });

});
